import Vue from 'vue'
import Vuex from 'vuex'
import {
	getParams,

} from '../api/system.js'
import {
	payCreate
} from "../api/payOrder.js"
import {
	getBindInfo,
	getUserId,
	changeUser,
	// userLogout,
	userLogin,
	userDetail,
	weixinCode,
	deletePatient,
	patientCardCancel
} from '../api/login.js'
import {
	patientCardStatus
} from '../api/login.js'
import {
	selectCampus
} from '../api/campus.js'
import link from '../static/js/config.js'
import {
	sessionG
} from '../utils/session.js'

Vue.use(Vuex)

const baseURL = process.env.NODE_ENV === 'development' ? link.dev.BASE_URL : link.env.BASE_URL
const store = new Vuex.Store({
	state: {
		systemParams: [], //系统配置
		// hostApi: "http://192.168.0.251:19150", // 后台服务器地址
		// hostApi: "/wxapi", // 外网统一后台服务器地址
		// hostApi: "http://192.168.99.131:19150", // 金堂后台服务器地址
		// hostApi: "http://192.168.0.251:19150", // 后台服务器地址
		// hostApi: "http://localhost:19150", // 后台服务器地址
		hostApi: 'http://192.168.1.105:19150', // 荣县后台服务器地址
		// hostApi: 'http://192.168.1.108:19150', // 荣县后台服务器地址
		// hostApi: "http://192.168.99.236:19150", // 后台服务器地址
		// hostApi: "http://192.168.3.18:19150", // 后台服务器地址
		// hostApi: 'http://localhost:19150', // 荣县后台服务器地址
		// hostApi: 'http://192.168.1.109:19150', // 荣县后台服务器地址
		// hostApi: 'http://192.168.43.200:19150', // 手机后台服务器地址
		// baseApi:baseURL + '/../wxapp/static/images/', // 图片展示路径前缀 picture/
		baseApi: location.origin + '/static/images/', // 图片展示路径前缀 picture/
		// baseApi: 'http://localhost/wxapp/static/images/', // 图片展示路径前缀 picture/
		bgApi: baseURL + '/web/file/', // 背景图片展示前缀
		userId: '',
		userInfo: {}, // 患者信息
		userList: [], // 当前患者绑定的就诊卡
		appId: '',
		hospitalInfo: '', // 医院信息
		healthCardType: '0',
		aiMessages: [], //ai访问数据
		aiGuidance: [], //ai智能医助
		currentHospitalInfo: {},
	},
	mutations: {
		SYSTEM_PARAMS_MU(state, systemParams) {
			state.systemParams = systemParams;
		},
		CURRENT_FIELD_MU(state, currentField) {
			state.curField = currentField;
		},
		SET_USERINFO(state, val) {
			state.userInfo = val;
		},
		SET_USERLIST(state, val) {
			state.userList = val;
		},
		SET_APPID(state, val) {
			state.appId = val;
		},
		SET_HOSPITAL_INFO(state, val) {
			state.hospitalInfo = val
		},
		SET_USERID(state, val) {
			state.userId = val;
		},
		SET_TX_CARD(state, val) {
			state.healthCardType = val
		},
		SET_AI_MESSAGES(state, val) {
			state.aiMessages = val
		},
		SET_AI_GUIDANCE(state, val) {
			state.aiGuidance = val
		},
		setCurrentHospitalInfo(state, val) {
			state.currentHospitalInfo = val
		},
		setNoticeContent(state, content) {
			state.noticeContent = content
		}
	},
	getters: {
		systemParams: state => state.systemParams || [], //系统参数
		hostApi: state => state.hostApi, // 基础路径
		baseApi: state => state.baseApi, // 图片展示路径前缀 基础路径
		bgApi: state => state.bgApi, // 背景图片展示前缀 展示图片的路径
		userInfo: state => state.userInfo, // 当前默认用户的信息
		userList: state => state.userList, // 当前微信或者支付宝用户所绑定用就诊卡列表
		hospitalInfo: state => state.hospitalInfo, // 医院信息
		userId: state => state.userId, // 引用唯一ID
		healthCardType: state => state.healthCardType, // 是否启用腾讯电子健康卡
		aiMessages: state => state.aiMessages, // ai访问数据
		aiGuidance: state => state.aiGuidance, // ai智能医助
		currentHospitalInfo: state => state.currentHospitalInfo, //当前选中的医院信息
	},
	actions: {
		setAiMessages({
			commit
		}, data) {
			commit("SET_AI_MESSAGES", data || [])
		},
		setAiGuidance({
			commit
		}, data) {
			commit("SET_AI_GUIDANCE", data || [])
		},
		// 获取系统配置参数
		getParams({
			commit
		}) {
			getParams().then(data => {
				commit('SYSTEM_PARAMS_MU', data || [])
				let res = data || [],
					isHealthCard = '0'
				res.forEach(item => {
					if (item.code === 'SFQYDZJKK') {
						isHealthCard = item.value
					}
				})
				if (isHealthCard)
					commit('SET_TX_CARD', isHealthCard)
			})
		},
		// 获取医院信息
		getHospitalInfo({
			commit
		}, data) {
			selectCampus({}).then(res => {
				commit('SET_HOSPITAL_INFO', res || '');
			})

		},
		// 获取用户id
		getUserId({
			commit,
			dispatch
		}, authCode) {
			return new Promise((resolve, reject) => {
				weixinCode({
					code: authCode
				}).then(async data => {
					if (data) {
						const result = data.find(item => item.loginStatus == 1);
						if (result) {
							commit('SET_USERID', result.openId || '');
						} else {
							commit('SET_USERID', '');
						}
						try {
							commit('SET_USERLIST', data);
							commit('SET_USERINFO', result);
							resolve()
						} catch (e) {
							reject()
						}
					} else {
						console.error('未获取到用户ID')
						reject()
					}
				}).catch(e => {
					reject()
				})
			})
		},
		// 获取当前用户绑定的用户信息列表
		getBindInfo({
			commit
		}, userId) {
			return new Promise((resolve, reject) => {
				console.log(userId)
				getBindInfo({
					openId: userId,
					status: 1,
					orderBy: 'login_status desc'
				}).then(data => {
					const list = data.map(item => {
						item.name = item.patName
						item.sex - item.patSex
						item.idCard = item.certId;
						item.cardNo = item.patId
						item.patId = item.patId
						return item;
					})
					commit('SET_USERLIST', list || []);
					const currentUser = data.filter(item => {
						item.name = item.patName
						item.sex - item.patSex
						item.idCard = item.certId;
						item.cardNo = item.patId
						item.patId = item.patId
						return item.loginStatus === 1;
					})
					commit('SET_USERINFO', currentUser[0] || '');
					if (this.state.userId) {

					} else {
						commit("SET_USERID", currentUser[0].openId)
					}
					resolve()
				}).catch(e => {
					reject()
				})
			})
		},
		// 切换就诊人
		changeUser({
			state,
			dispatch
		}, newPatId) {
			return new Promise((resolve, reject) => {
				let oldPatId = state.userInfo ? state.userInfo.patId : ''
				let params = {
					oldPatId,
					newPatId
				}
				changeUser(params).then(async data => {
					try {
						await dispatch("getBindInfo", state.userId);
						resolve(true)
					} catch (e) {
						reject(false)
					}
				}).catch(e => {
					reject(false)
				})
			})
		},
		setUserList({
			commit
		}, data) {

			commit('SET_USERLIST', data || []);
			const currentUser = data.filter(item => item.loginStatus === 1)
			console.log(currentUser)
			if (currentUser.length > 0) {
				commit('SET_USERINFO', currentUser[0] || '');
				commit("SET_USERID", currentUser[0].openId)
			}

			console.log(store.state.userList)
			console.log(store.state.userInfo)
		},
		// 用户解绑
		userLogout({
			dispatch,
			state
		}, userInfo) {
			return new Promise((resolve, reject) => {
				patientCardStatus(userInfo).then(data => {
					dispatch("getBindInfo", state.userId);
					resolve(true)
				}).catch(e => {
					reject(false)
				})
			})
		},
		// 用户绑卡
		userLogin({
			dispatch,
			state
		}, params) {
			return new Promise((resolve, reject) => {
				params.userId = state.userId
				userLogin(params).then(data => {
					// dispatch("getBindInfo", state.userId); 登录页面绑卡已在页面中处理不需要重复调用
					resolve(true)
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 用户查询详情信息
		userDetail({
			state
		}, patId) {
			return new Promise((resolve, reject) => {
				userDetail({
					patId
				}).then(data => {
					resolve(data)
				}).catch(e => {
					reject(false)
				})
			})
		},
		// 创建统一下单
		createPay({
			state
		}, params) {
			return new Promise((resolve, reject) => {
				payCreate(params).then(data => {
					resolve(data)
				}).catch(e => {
					reject(false)
				})
			})
		},
		setCurrentHospitalInfo({
			commit
		}, data) {
			commit("setCurrentHospitalInfo", data);
		}
	}
})

export default store